package com.cj.springmvc.test.task.mdp;

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
import org.jboss.logging.Logger;
import org.springframework.stereotype.Component;

import com.cj.springmvc.test.dto.PersonsDto;
/**
 *  a pure POJO <br>
 *  通过xml配置，完全与Spring API 相分离<br>
 *  作为一个listener，异步完成从JMS服务器中获得指定queue或者topic的信息，并执行业务操作<br>
 *  具体配置见example-jms-temp.xml
 *  @author cj
 * */
@Component("personsHandler")
public class PersonsHandler {
	private Logger log = Logger.getLogger(PersonsHandler.class);
	
	public void processPersons(PersonsDto personDto){
		// 通过JMS 接收到服务器指定的queue信息，进行邮件发送操作！
		log.info("信息完成接受,用户名为:" + personDto.getUsername() +
				",邮箱为:" + personDto.getEmail());
		Email email = getEmail();
		
		try {
			email.addTo(personDto.getEmail(), personDto.getUsername());
			email.setFrom("caojian43710@163.com");
			email.setSubject("Weclome To My Zone!");
			email.setMsg("Welcome to be one of us!");
			email.send();
		} catch (EmailException e) {
			e.printStackTrace();
		}
	}
	
	private Email getEmail(){
		Email email = new SimpleEmail();
		email.setAuthenticator(new DefaultAuthenticator("caojian43710", "252084886cic"));
		email.setHostName("smtp.163.com");
		return email;
	}
}
